{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AG2 Wikipedia Search Tools\n",
    "\n",
    "AG2's Wikipedia search integration allows agents to perform searches in Wikipedia and retrieve the relevant pages. Follow these steps to integrate Wikipedia Search Tools with AG2 Agents.\n",
    "\n",
    "Two tools are available for your AG2 agents:\n",
    "- `WikipediaQueryRunTool` executes Wikipedia queries and returning summarized page results\n",
    "- `WikipediaPageLoadTool` loads the contents of a Wikipedia page together with its metadata (for detailed data extraction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install required dependencies\n",
    "%pip install agentops\n",
    "%pip install \"ag2[wikipedia, openai]\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "import agentops\n",
    "from autogen import AssistantAgent, LLMConfig, UserProxyAgent\n",
    "from autogen.tools.experimental import WikipediaPageLoadTool, WikipediaQueryRunTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv()\n",
    "os.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_api_key_here\")\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"your_openai_api_key_here\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Agent Configuration\n",
    "\n",
    "Configure an assistant agent and user proxy to be used for LLM recommendation and execution respectively.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agentops.init(auto_start_session=False)\n",
    "tracer = agentops.start_trace(\n",
    "    trace_name=\"AG2 Wikipedia Search Tools\", tags=[\"ag2-wikipedia-search-tools\", \"agentops-example\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_list = LLMConfig(api_type=\"openai\", model=\"gpt-4o-mini\")\n",
    "\n",
    "assistant = AssistantAgent(\n",
    "    name=\"assistant\",\n",
    "    llm_config=config_list,\n",
    ")\n",
    "\n",
    "user_proxy = UserProxyAgent(name=\"user_proxy\", human_input_mode=\"NEVER\", code_execution_config=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Query Tool Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wikipedia_query_tool = WikipediaQueryRunTool()\n",
    "\n",
    "# Register the tool for LLM recommendation (assistant agent) and execution (user_proxy agent).\n",
    "wikipedia_query_tool.register_for_llm(assistant)\n",
    "wikipedia_query_tool.register_for_execution(user_proxy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start the Conversation\n",
    "\n",
    "With the setup complete, you can now use the assistant to search Wikipedia."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = user_proxy.initiate_chat(\n",
    "    recipient=assistant,\n",
    "    message=\"Who is the father of AI?\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Page Load Tool Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Start by removing the Query tool so we ensure our agent uses the Page Load tool\n",
    "assistant.remove_tool_for_llm(wikipedia_query_tool)\n",
    "\n",
    "# Create the Page Load tool\n",
    "wikipedia_page_load_tool = WikipediaPageLoadTool()\n",
    "\n",
    "# Register the tool for LLM recommendation (assistant agent) and execution (user_proxy agent).\n",
    "wikipedia_page_load_tool.register_for_llm(assistant)\n",
    "wikipedia_page_load_tool.register_for_execution(user_proxy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = user_proxy.initiate_chat(\n",
    "    recipient=assistant,\n",
    "    message=\"What's the population of Australia?\",\n",
    "    max_turns=2,\n",
    ")\n",
    "\n",
    "agentops.end_trace(tracer, end_state=\"Success\")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Perplexity Search Integration with AG2",
   "tags": [
    "tools",
    "perplexity",
    "web-search",
    "search"
   ]
  },
  "kernelspec": {
   "display_name": "agentops (3.11.11)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
